package com.ahua.controller;

import com.ahua.entity.ObjectItem;
import com.ahua.service.MinioService;
import com.ahua.service.impl.MinioServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Y.C
 * @CreateTime: 2022/5/20 16:57
 * @Description: minio控制器
 */
@RestController
@RequestMapping("/minio")
public class MinioController {
    @Autowired
    private MinioService minioService;

    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.bucketName}")
    private String bucketName;

    /**
     * 批量上传文件
     * @param files
     * @return
     */
    @PostMapping("/uploads")
    public List<String> uploads(MultipartFile[] files) {
        List<String> fileUrls = new ArrayList<>();
        List<String> fileNames = minioService.uploads(files);
        for (String fileName : fileNames) {
            fileUrls.add(endpoint + "/" + bucketName + "/" + fileName);
        }
        return fileUrls;
    }

    /**
     * 获取文件对象列表
     * @return
     */
    @GetMapping("/listObjects")
    public List<ObjectItem> listObjects() {
        List<ObjectItem> objectItems = minioService.listObjects(bucketName);
        return objectItems;
    }
}
